<template>
  <div id="app2">
    <el-container>
      <el-aside width="200px">
        <el-row class="tac">
          <el-col :span="12" id="left">
            <el-menu
              default-active="2"
              class="el-menu-vertical-demo"
              @open="handleOpen"
              @close="handleClose"
              :default-openeds="['2']"
            >
              <el-menu-item index="3">
                <span slot="title">欢迎光临:&nbsp;{{ username }}</span>
              </el-menu-item>
              <el-menu-item index="1">
                <i class="el-icon-menu"></i>
                <span slot="title" @click="shouye">首页</span>
              </el-menu-item>

              <el-submenu index="2">
                <template slot="title">
                  <i class="el-icon-document"></i>
                  <span>页面管理</span>
                </template>
                <el-menu-item-group>
                  <el-menu-item
                    class="span"
                    index="1-4"
                    id="el"
                    @click="myshop4"
                    >食品管理</el-menu-item
                  >
                  <el-menu-item index="1-5" id="el" @click="myshop5"
                    >订单管理</el-menu-item
                  >
                </el-menu-item-group>
              </el-submenu>
            </el-menu>
          </el-col>
        </el-row>
      </el-aside>
      <el-main>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item><a href="/">食品管理</a></el-breadcrumb-item>
          <el-dropdown>
            <span class="el-dropdown-link">
              个人中心<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人资料</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item @click="tuichu">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-breadcrumb>

        <div class="formwrap">
          <h2 class="addtitle">添加食品</h2>
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="名称" prop="goodsname">
              <el-input
                type="text"
                v-model="ruleForm.goodsname"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item label="介绍" prop="goodsintroduction">
              <el-input
                type="text"
                v-model="ruleForm.goodsintroduction"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item label="价格" prop="goodsprice">
              <el-input
                type="number"
                min="1"
                v-model="ruleForm.goodsprice"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item label="食品图片">
              <el-upload
               v-if="ruleForm.goodsimg == ''"
                class="upload-demo"
                drag
                name="filename"
                action="/api/uploads"
                :on-success="uploadSuccess"
                multiple
              >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                  将文件拖到此处，或<em>点击上传</em>
                </div>
                <div class="el-upload__tip" slot="tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
              </el-upload>
              <img class="goodsimg" v-else :src="ruleForm.goodsimg" alt="">
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >提交</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import dsgoodsModel from "../../model/dsgoodsModel";
import * as echarts from "echarts";
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      input: "",
      username: "",
      tableData: [],
      ruleForm: {
        goodsname:'',
        goodsintroduction:'',
        goodsimg:'',
        goodsprice:'',
      },
      rules: {
        goodsname :[{ required: true, message: '请输入食品名称', trigger: 'blur' }],
        goodsintroduction :[{ required: true, message: '请输入食品介绍', trigger: 'blur' }],
        goodsprice :[{ required: true, message: '请输入食品价格', trigger: 'blur' }],
        goodsimg:[{ required: true, message: '请上传图片', trigger: 'blur' }]
      },
    };
  },
  async created() {
    let info = window.localStorage.getItem("info");
    this.username = info;
  },
  methods: {
    uploadSuccess(response, file, fileList){
      this.ruleForm.goodsimg = response.url
        console.log(response)
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if(this.ruleForm.goodsimg){
            let resulte = await dsgoodsModel.add(this.ruleForm)
             this.$message({
            type: "success",
            message: "添加成功!",
          });
          setTimeout(() => {
              this.$router.push("/ds/shop/food")
          }, 500);
          }else{
             this.$message.error('请上传图片');
          }
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.tableData = this.tableData.filter((item) => item._id != id);
          await dsgoodsModel.delete(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    myshop4() {
      this.$router.push("/ds/shops/food");
    },
    myshop5() {
      this.$router.push("/ds/shops/order");
    },
    shouye() {
      this.$router.push("/ds/shops");
    },
    tuichu() {},
    handleOpen() {},
    handleClose() {},
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
  },
};
</script>

<style>
/* #el {
  color: #fff;
} */

.goodsimg{
  width: 200px;
  height: auto;
}
.addtitle {
  text-align: center;
  margin-bottom: 20px;
}
.el-main .el-button--primary span {
  color: #fff;
}
.formwrap {
  width: 60%;
}
.el-message-box,
.el-message-box span {
  color: #000;
}

.el-main span {
  color: #000;
}
.el-message-box__btns button:nth-child(2) span {
  color: #fff;
}
.div {
  margin-bottom: 20px;
}
.el-breadcrumb {
  border-bottom: 1px solid gray;
  margin-bottom: 20px;
}
.el-main {
  padding: 0;
  margin-left: 20px;
}
.el-aside {
  overflow: hidden;
}
#chart1,
#chart2,
#chart3,
#chart4 {
  height: 350px;
  margin: 20px 0;
}
.el-row {
  margin: 0 !important;
}
.el-aside {
  position: fixed;
}
.el-main {
  padding-left: 200px;
}
</style>
